package stringPractice;

import java.util.Arrays;

public class StringPractice {

      /*
            将字符串"aaaaaaabbbbbcerrrrggggggggsssssspoqqqqq"压缩处理成"a7b5cer4g8s6poq5"
       */
      public static void main(String[] args) {
            String str = "aaaaaaabbbbbcerrrrggggggggsssssspoqqqqq";
            String s = handle1(str);
            System.out.println(Arrays.toString(returnChar(str)));
            System.out.println("=========");
            int[] chare = returnChar(str);
            String s1 = returnString(chare, str);
            System.out.println(s);
            System.out.println("=========");
            System.out.println(s1);
      }

      //获取字符shuzu
      public static int[] returnChar(String str) {
            int[] result = new int[26];
            char a = 'a';
            for (int i = 0; i < 26; i++) {
                  int count = 0;
                  for (char c : str.toCharArray()) {
                        if (a == c) {
                              count++;
                        }
                  }
                  result[i] = count;
                  a = (char) (a + 1);
            }
            return result;
      }

      //获取拼接的字符串
      public static String returnString(int[] chars, String str) {
            String str3 = "";
            for (int i = 0; i < chars.length; i++) {
                  if (chars[i] > 1) {
                        str3 = str3.concat((char) ('a' + i) + String.valueOf(chars[i]));
                  } else if (chars[i] == 1) {
                        str3 = str3.concat(String.valueOf((char) ('a' + i)));
                  }
            }
            return str3;
      }

      public static String handle1(String str) {
            int[] array = new int[26];
            String str1 = "";

            for (int i = 0; i < str.length(); i++) {
                  array[str.charAt(i) - 'a']++;//每一个字符-'a',得到的是这个字母在这个数组中对应下标所出现的次数，str.charAt(i)表示遍历到的字符，array[]
            }
            for (int i = 0; i < array.length; i++) {
                  if (array[i] == 1) {
                        str1 = str1.concat(String.valueOf((char) ('a' + i)));
                  } else if (array[i] > 1) {
                        str1 = str1.concat((char) ('a' + i) + String.valueOf(array[i]));
                  }
            }
            return str1;
      }
}
